<template>
  <div>
    <!-- 登录 导航 -->
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="
        () => {
          $router.push('/');
        }
      "
    />
    <!-- from 表单 -->
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="LoginFrom.username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="LoginFrom.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="LoginFrom.phone"
          type="phone"
          name="手机号"
          label="手机号"
          placeholder="手机号"
          :rules="[{ pattern, message: '请填写手机号' }]"
        />
        
        <!-- 秒 -->
        <van-count-down :time="time" format="倒计时:60 秒" @click="dj"/>
      </van-cell-group>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="primary"
          native-type="submit"
          @click="submit"
        >
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
// 跳转
import { useRouter } from "vue-router";
let router = new useRouter();
// 引入登录函数
import { loginApi } from "@/api/api";
// 引入轻提示
import { showToast } from "vant";
let LoginFrom = reactive({
  username: "",
  password: "",
});
const pattern = /^[0-9]{3,11}$/
const submit = () => {
  loginApi(LoginFrom.username, LoginFrom.password).then((res) => {
    console.log("登录", res);

    if (res.data.code == 1) {
      showToast({
        duration: 3000,
        type:'success',
        message: "登录成功",
        onClose(){
            router.push('/')
        }
      });
    }
    localStorage.setItem('username',LoginFrom.username);
    localStorage.setItem('token',res.data.data.token);
  });
  router.push('/home/my')
};
const dj = () =>{
    time--;
}
</script>

<style lang="scss" scoped>
</style>